<template>
  <div>
    <!-- 新增视频模板 -->
    <el-dialog title="新增视频" :visible.sync="addDialogVisible" width="38%" @close="addDialogClosed">
      <el-alert title="请开始你的操作" type="info" center show-icon :closable="false"></el-alert>

      <el-steps :space="200" :active="activeIndex - 0" finish-status="success" align-center>
        <el-step title="基本信息"></el-step>
        <el-step title="文件上传"></el-step>
        <el-step title="描述信息"></el-step>
        <el-step title="完成"></el-step>
      </el-steps>

      <el-form
        :model="addForm"
        :rules="addFormRules"
        ref="addFormRef"
        label-width="70px"
        label-position="top"
      >
        <el-tabs v-model="activeIndex" :tab-position="'left'" :before-leave="beforeTabLeave">
          <el-tab-pane label="基本信息" name="0">
            <el-form-item label="视频名称" prop="videoName">
              <el-input v-model="addForm.videoName" style="width:51%"></el-input>
            </el-form-item>
            <el-form-item label="视频类型" prop="typeId">
              <el-select v-model="addForm.typeId" placeholder="请选择视频类型">
                <el-option
                  v-for="item in videoTypes"
                  :key="item.id"
                  :label="item.naviName"
                  :value="item.id"
                ></el-option>
              </el-select>
            </el-form-item>
            <el-form-item label="视频状态" prop="status">
              <el-select v-model="addForm.status" placeholder="请选择视频状态">
                <el-option
                  v-for="item in options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="文件上传" name="1">
            <el-form-item label="上传图片" prop="imageId">
              <el-upload
                action="http://api.coco.com/api/upload/image"
                :on-preview="handlePreview"
                :on-remove="handleRemove"
                list-type="picture"
                :on-error="handleError"
                :on-success="handleSuccess"
              >
                <el-button size="small" type="primary">点击上传</el-button>
                <div slot="tip" class="el-upload__tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-form-item>
            <el-form-item label="上传视频" prop="videoUrl">
              <el-upload
                class="upload-demo"
                drag
                action="http://api.coco.com/api/upload/video"
                :on-success="handleSuccess1"
                :on-error="handleError1"
                :on-remove="handleRemove1"
              >
                <i class="el-icon-upload"></i>
                <div class="el-upload__text">
                  将文件拖到此处，或
                  <em>点击上传</em>
                </div>
                <div class="el-upload__tip" slot="tip">只能上传jpg/png文件，且不超过500kb</div>
              </el-upload>
            </el-form-item>
          </el-tab-pane>
          <el-tab-pane label="描述信息" name="2">
            <el-form-item label="上传视频" prop="remark">
              <el-input type="textarea" v-model="addForm.remark" :rows="6"></el-input>
              <el-button type="warning" @click="addVideo()">完成</el-button>
            </el-form-item>
          </el-tab-pane>
        </el-tabs>
      </el-form>
    </el-dialog>
    
    <!-- 收藏列表 -->
    <el-dialog title="收藏列表" :visible.sync="dialogVisible" width="30%">
      <el-table :data="collectionList" stripe style="width: 100%">
        <el-table-column prop="videoName" label="视频名称"></el-table-column>
        <el-table-column prop="typeId" label="视频类型">
          <template slot-scope="scope">
            <span v-text="getTypeName(scope.row.typeId)"></span>
          </template>
        </el-table-column>
        <el-table-column prop="status" label="状态"></el-table-column>
        <el-table-column prop="updateTime" label="更新时间" width="100px">
          <template slot-scope="scope">
            <span v-text="$moment(scope.row.updateTime).format('YYYY-MM-DD')"></span>
          </template>
        </el-table-column>
        <el-table-column prop="id" label="更新时间">
          <template slot-scope="scope">
            <router-link :to="'/video/' + scope.row.id">
            <el-button type="primary" round size="small">跳转</el-button>
            </router-link>
          </template>
        </el-table-column>
      </el-table>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false">取 消</el-button>
      </span>
    </el-dialog>

    <!-- 用户详情 -->
    <el-dialog title="用户详情" :visible.sync="userDialogVisible" width="30%">
          <span>用户名{{userDatil.name}}</span>
          <el-divider></el-divider>
          <span>手机号：{{userDatil.phone}}</span>
          <el-divider></el-divider>
          <span>邮箱：{{userDatil.email}}</span>
          <el-divider></el-divider>
          <span>年龄：{{userDatil.age}}</span>
          <el-divider></el-divider>
          <span>出生年月：{{userDatil.birthday}}</span>
          <el-divider></el-divider>
          <span>性别：{{userDatil.sex}}</span>
          <el-divider></el-divider>
          <span>家庭住址：{{userDatil.address}}</span>
          <el-divider></el-divider>
          <span>爱好：{{userDatil.like}}</span>
          <el-divider></el-divider>
          <span>个性签名：{{userDatil.signature}}</span>
          <el-divider></el-divider>
          <span slot="footer" class="dialog-footer">
            <el-button @click="userDialogVisible = false">取 消</el-button>
          </span>
        </el-dialog>
  </div>
</template>

<script>
import Conversion from "../assets/js/conversion";
export default {
  data() {
    return {
      options: [
        {
          value: "上架",
          label: "上架"
        },
        {
          value: "下架",
          label: "下架"
        }
      ],
      queryInfo: {
        query: {
          keyWord: "",
          typeId: [],
          times: []
        },
        pageIndex: 1,
        pageSize: 3
      },
      total: 0,
      videosList: [],
      videoTypes: [],
      searchDialogVisible: false,
      addDialogVisible: false,
      editDialogVisible: false,
      dialogVisible: false,
      userDialogVisible: false,
      previewVisible: false,
      previewPath: "",
      activeIndex: "0",
      activeIndex1: "0",
      addForm: {},
      editForm: {},
      userDatil:{},
      searchFormRules: {
        keyWord: [
          { min: 2, max: 5, message: "长度在2到5个字符之间", trigger: "blur" }
        ]
      },
      addFormRules: {
        videoName: [
          { required: true, message: "请输入视频名称", trigger: "blur" },
          { min: 2, max: 5, message: "长度在2到5个字符之间", trigger: "blur" }
        ]
      },
      editFormRules: {},
      collectionList: []
    };
  },
  created() {
    this.getNavis();
    this.getCollectionInfo();
  },
  methods: {
    async getCollectionInfo() {
      await this.$http
        .get("/portal/video/selectCollectionByUserId")
        .then(resp => {
          if (resp.status !== 200) return this.$message.error("获取数据失败！");
          this.collectionList = resp.data;
        });
    },
    addDialogClosed() {
      this.$refs.addFormRef.resetFields();
      this.activeIndex = "0";
    },
    async getNavis() {
      const res = await this.$http.get("/manager/navi/getNavis");
      if (res.status !== 200) {
        return this.$message.error("获取类型列表失败!");
      }
      console.log(res.data);
      this.videoTypes = res.data;
    },
    getTypeName(id) {
      var a = "";
      this.videoTypes.forEach(element => {
        if (element.id === id) {
          a = element.naviName;
        }
      });
      return a;
    },
    async openDatil(){
      const res = await this.$http.get(
        "/manager/user/getUserInfoById");
      if (res.status !== 200) {
        return this.$message.error("获取用户列表失败!");
      }
      this.userDatil = res.data;
      this.userDialogVisible = true
    },
    addVideo() {
      this.$refs.addFormRef.validate(async valid => {
        if (!valid) return;
        await this.$http
          .post("/manager/video/save", this.addForm)
          .then(resp => {
            if (resp.status !== 201) return this.$message.error("添加失败");
            this.$message.success("添加成功");
            this.addDialogVisible = false;
            //调用其他页面方法组件
            Conversion.$emit("H_refresh");
            Conversion.$emit("V_refresh");
          })
          .catch(() => {
            this.$message.error("异常！");
          });
      });
    },
    beforeTabLeave(activeName, oldActiveName) {
      console.log(this.addForm.videoName);
      if (oldActiveName === "0" && this.addForm.videoName === undefined) {
        this.$message.error("请填写名称");
        return false;
      }

      // if (
      //   oldActiveName === "1" &&
      //   (this.addForm.imageId === undefined ||
      //     this.addForm.videoUrl === undefined)
      // ) {
      //   console.log(oldActiveName);
      //   this.$message.error("请上传文件");
      //   return false;
      // }
    },
    //新增的文件相关操作
    handlePreview(file) {
      console.log(file.response);
      this.previewPath = file.response;
      this.previewVisible = true;
    },
    handleRemove(file) {
      this.addForm.imageId = undefined;
    },
    handleSuccess(resp) {
      this.addForm.imageId = resp;
    },
    handleRemove1(file) {
      this.addForm.videoUrl = undefined;
    },
    handleSuccess1(resp) {
      this.addForm.videoUrl = resp;
    },

    handleError() {
      this.$message.error("请上传图片");
    },
    handleError1() {
      this.$message.error("请上传视频");
    },
    init() {
      this.addDialogVisible = true;
    },
    initCollection() {
      this.dialogVisible = true;
    },
    initInfo(){
      this.openDatil()
      this.userDialogVisible = true;
    }
  }
};
</script>

<style lang="less" scoped>
</style>